import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_widgets/config/kq_global.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_widgets/utils/dialog_util.dart';
import 'package:kq_flutter_core_widget/utils/str_util.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_widgets/widgets/bottomSheet/kq_botton_sheet_manger.dart';
import 'package:kq_flutter_widgets/widgets/bottomSheet/sheets/kq_action_list_bottom_sheet.dart';
import 'package:kq_flutter_widgets/widgets/bottomSheet/sheets/kq_date_picker_bottom_sheet.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_outline_button.dart';
import 'package:kq_flutter_widgets/widgets/dialog/kq_choose_dialog.dart';
import 'package:kq_flutter_widgets/widgets/formItem/kq_empty_item_view.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';

/// 对话框演示
class DialogDemoPage extends StatefulWidget {
  const DialogDemoPage({Key? key}) : super(key: key);

  @override
  DialogDemoPageState createState() => DialogDemoPageState();
}

class DialogDemoPageState extends State<DialogDemoPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: '对话框演示',
        back: () {
          Get.back();
        },
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(
                    child: Wrap(
                      spacing: 10.r,
                      runSpacing: 10.r,
                      children: [
                        KqOutlineButton(
                          title: '消息对话框',
                          onTap: () {
                            KqDialog.showMsgDialog(
                                title: '提示',
                                msg: '任务单删除成功任务单删除成功任务单删除成功任务单删除成功任务单删除成功！');
                          },
                        ),
                        KqOutlineButton(
                          title: '确认对话框',
                          onTap: () {
                            KqDialog.showConfirmDialog(
                              title: '删除任务单',
                              msg: '确定删除该任务单吗？',
                              onConfirm: () {
                                KqToast.showNormal('删除成功！');
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '中间列表对话框',
                          onTap: () {
                            KqDialog.showListDialog(
                              [
                                "很长很长很长很长很长很长很长很长长很长长很长",
                                "222",
                                "333",
                                "444",
                                "555",
                                "666",
                                "777",
                                "888"
                              ],
                              searchEnable: false,
                              onSearchItem: (searchText, item) {
                                return item.contains(searchText);
                              },
                              callback: (index, item) {
                                KqToast.showNormal(item);
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '底部列表对话框',
                          onTap: () {
                            KqBottomSheetManager.showActionList([
                              KqActionListData(
                                  item: '很长很长很长很长很长很长很长很长长很长长很长很长长很长很长长很长',
                                  itemId: '111'),
                              KqActionListData(item: '222'),
                              KqActionListData(
                                  item: '333',
                                  textColor: KqThemeColors.textRed),
                              KqActionListData(item: '444'),
                              KqActionListData(
                                  item: '555',
                                  textColor: KqThemeColors.textYellow),
                              KqActionListData(item: '666'),
                              KqActionListData(item: '777'),
                              KqActionListData(item: '888'),
                            ], title: '标题内容',
                                callback: (int index, KqActionListData item) {
                              KqToast.showNormal(item.item);
                            });
                          },
                        ),
                        KqOutlineButton(
                          title: '输入对话框（单行）',
                          onTap: () {
                            KqDialog.showInputDialog(
                              '请输入产品名称',
                              onEmptyCallback: () =>
                                  KqToast.showNormal('产品名称不能为空'),
                              onConfirm: (value) {
                                KqToast.showNormal(value);
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '输入对话框(多行)',
                          onTap: () {
                            KqDialog.showInputDialog(
                              '请输入撤回原因',
                              multiline: true,
                              middleBtnText: '测试中间',
                              onMiddleBtnClick: (value) {
                                KqToast.showNormal('中间点击$value');
                              },
                              onConfirm: (value) {
                                KqToast.showNormal(value);
                              },
                            );
                          },
                        ),
                        // KqOutlineButton(
                        //   title: '日期选择',
                        //   onTap: () {
                        //     KqDialog.showDatePicker(
                        //       context,
                        //       '选择日期',
                        //       initialDateTime: DateTime.parse('2021-09-08'),
                        //       onConfirm: (dateTime, selectedIndex) {
                        //         KqToast.showNormal(
                        //             StrUtil.formatDatetimeYMD(dateTime));
                        //         return true;
                        //       },
                        //     );
                        //   },
                        // ),
                        // KqOutlineButton(
                        //   title: '时间选择',
                        //   onTap: () {
                        //     KqDialog.showTimePicker(
                        //       context,
                        //       '选择时间',
                        //       onConfirm: (dateTime, selectedIndex) {
                        //         KqToast.showNormal(
                        //             StrUtil.formatDatetimeHms(dateTime));
                        //         return true;
                        //       },
                        //     );
                        //   },
                        // ),
                        // KqOutlineButton(
                        //   title: '日期时间选择',
                        //   onTap: () {
                        //     KqDialog.showDateTimePicker(
                        //       context,
                        //       '选择日期和时间',
                        //       onConfirm: (dateTime, selectedIndex) {
                        //         KqToast.showNormal(
                        //             StrUtil.formatDatetimeYMDHms(dateTime));
                        //         return true;
                        //       },
                        //     );
                        //   },
                        // ),
                        KqOutlineButton(
                          title: '日期选择底部',
                          onTap: () {
                            KqBottomSheetManager.showDatePicker(
                              dateModes: [
                                KqDatePickerMode.byDay,
                                KqDatePickerMode.byMonth,
                                KqDatePickerMode.byYear
                              ],
                              defDateMode: KqDatePickerMode.byMonth,
                              onConfirm: (dateTime, selectedIndex) {
                                KqToast.showNormal(
                                    StrUtil.formatDatetimeYMD(dateTime));
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '时间选择底部',
                          onTap: () {
                            KqBottomSheetManager.showTimePicker(
                              onConfirm: (dateTime, selectedIndex) {
                                KqToast.showNormal(
                                    StrUtil.formatDatetimeHms(dateTime));
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '日期时间选择底部',
                          onTap: () {
                            KqBottomSheetManager.showDateTimePicker(
                              onConfirm: (dateTime, selectedIndex) {
                                KqToast.showNormal(
                                    StrUtil.formatDatetimeYMDHms(dateTime));
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '类似原因对话框',
                          onTap: () {
                            KqDialog.showReasonDialog(
                              title: '选择返修原因',
                              renderIdCallback: (index) {
                                return '$index';
                              },
                              chosenDataId: '1',
                              renderTitleCallback: ((index) {
                                return '$index索引索引索引索引索引索';
                              }),
                              totalCount: 40,
                              callBack: (index) {
                                print('点击了这一行---------$index');
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '日期范围选择底部',
                          onTap: () {
                            KqBottomSheetManager.showDateRangePicker(
                              hintText: '最长可查找时间跨度XX的任务单',
                              onConfirm: (startTime, endTime) {
                                if (startTime.millisecondsSinceEpoch >
                                    endTime.millisecondsSinceEpoch) {
                                  KqToast.showNormal('开始日期不能大于结束日期');
                                  return false;
                                }
                                KqToast.showNormal(
                                    "${StrUtil.formatDatetimeYMD(startTime)}到${StrUtil.formatDatetimeYMD(endTime)}");
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '时间范围选择底部',
                          onTap: () {
                            KqBottomSheetManager.showTimeRangePicker(
                              hintText: '最长可查找时间跨度XX的任务单',
                              onConfirm: (startTime, endTime) {
                                if (startTime.millisecondsSinceEpoch >
                                    endTime.millisecondsSinceEpoch) {
                                  KqToast.showNormal('开始时间不能大于结束时间');
                                  return false;
                                }
                                KqToast.showNormal(
                                    "${StrUtil.formatDatetimeHms(startTime)}到${StrUtil.formatDatetimeHms(endTime)}");
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '日期时间范围选择底部',
                          onTap: () {
                            KqBottomSheetManager.showDateTimeRangePicker(
                              hintText: '最长可查找时间跨度XX的任务单',
                              withMinute: false,
                              withSecond: false,
                              resultFontSize: 14.sp,
                              onConfirm: (startTime, endTime) {
                                if (startTime.millisecondsSinceEpoch >
                                    endTime.millisecondsSinceEpoch) {
                                  KqToast.showNormal('开始时间不能大于结束时间');
                                  return false;
                                }
                                KqToast.showNormal(
                                    "${StrUtil.formatDatetimeYMDHms(startTime)}到${StrUtil.formatDatetimeYMDHms(endTime)}");
                                return true;
                              },
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '多层级选择底部',
                          onTap: () {
                            KqBottomSheetManager.showLinkagePicker(
                                title: "选择控制器厂家和控制器型号",
                                totalLevel: 2,
                                showWhatInItem: (level, item) {
                                  switch (level) {
                                    case 0:
                                      return item['controllerVendor'];
                                    case 1:
                                      return item['controllerType'];
                                  }
                                  return '';
                                },
                                onFillData: (level, lastLevelData,
                                    lastLevelSelect, lastLevelItem) {
                                  switch (level) {
                                    case 0:
                                      return linkList;
                                    case 1:
                                      return lastLevelItem[
                                          'controllerTypeData'];
                                  }
                                  return [];
                                },
                                onConfirm: (indexList, selectData) {
                                  KqToast.showNormal(
                                      "控制器厂家: ${selectData[0]['controllerVendor']} 控制器型号: ${selectData[1]['controllerType']}");
                                  return true;
                                });
                          },
                        ),
                        KqOutlineButton(
                          title: '自定义弹窗',
                          onTap: () {
                            KqDialog.showCustomDialog(
                              title: '测试',
                              customWidget: Container(
                                width: 300.r,
                                height: 200.r,
                              ),
                            );
                          },
                        ),
                        KqOutlineButton(
                          title: '自定义选择框(多选)',
                          onTap: () {
                            KqDialog.showChooseDialog(
                                title: '测试',
                                items: [
                                  KqChooseData(name: 'name', itemId: '001'),
                                  KqChooseData(name: 'name002', itemId: '002'),
                                  KqChooseData(name: 'name003', itemId: '004'),
                                  KqChooseData(name: 'name004', itemId: '004'),
                                  KqChooseData(name: 'name005', itemId: '005'),
                                  KqChooseData(name: 'name006', itemId: '006'),
                                  KqChooseData(name: 'name007', itemId: '007'),
                                  KqChooseData(name: 'name008', itemId: '008'),
                                  KqChooseData(name: 'name009', itemId: '009'),
                                  KqChooseData(name: 'name010', itemId: '010'),
                                  KqChooseData(name: 'name011', itemId: '011'),
                                  KqChooseData(name: 'name012', itemId: '012'),
                                  KqChooseData(name: 'name013', itemId: '013'),
                                  KqChooseData(name: 'name014', itemId: '014'),
                                  KqChooseData(name: 'name015', itemId: '015'),
                                  KqChooseData(name: 'name016', itemId: '016'),
                                  KqChooseData(name: 'name017', itemId: '017'),
                                  KqChooseData(name: 'name018', itemId: '018'),
                                  KqChooseData(name: 'name019', itemId: '019'),
                                  KqChooseData(name: 'name020', itemId: '020'),
                                  KqChooseData(name: 'name021', itemId: '021'),
                                  KqChooseData(name: 'name022', itemId: '022'),
                                ],
                                isSingleChoose: false,
                                callback: ((chooses) {
                                  print(chooses);
                                }));
                          },
                        ),
                        KqOutlineButton(
                          title: '自定义选择框(单选)',
                          onTap: () {
                            KqDialog.showChooseDialog(
                                title: '测试',
                                items: [
                                  KqChooseData(name: 'name', itemId: '001'),
                                  KqChooseData(name: 'name002', itemId: '002'),
                                  KqChooseData(name: 'name003', itemId: '004'),
                                  KqChooseData(name: 'name004', itemId: '004'),
                                  KqChooseData(name: 'name005', itemId: '005'),
                                  KqChooseData(name: 'name006', itemId: '006'),
                                  KqChooseData(name: 'name007', itemId: '007'),
                                  KqChooseData(name: 'name008', itemId: '008'),
                                  KqChooseData(name: 'name009', itemId: '009'),
                                  KqChooseData(name: 'name010', itemId: '010'),
                                  KqChooseData(name: 'name011', itemId: '011'),
                                  KqChooseData(name: 'name012', itemId: '012'),
                                  KqChooseData(name: 'name013', itemId: '013'),
                                  KqChooseData(name: 'name014', itemId: '014'),
                                  KqChooseData(name: 'name015', itemId: '015'),
                                  KqChooseData(name: 'name016', itemId: '016'),
                                  KqChooseData(name: 'name017', itemId: '017'),
                                  KqChooseData(name: 'name018', itemId: '018'),
                                  KqChooseData(name: 'name019', itemId: '019'),
                                  KqChooseData(name: 'name020', itemId: '020'),
                                  KqChooseData(name: 'name021', itemId: '021'),
                                  KqChooseData(name: 'name022', itemId: '022'),
                                ],
                                isSingleChoose: true,
                                callback: ((chooses) {
                                  print(chooses);
                                }));
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  var linkList = [
    {
      "controllerTypeData": [
        {"controllerType": "KEBASMR", "controllerTypeId": "KEBASMR"},
        {"controllerType": "KEBABC", "controllerTypeId": "KEBABC"},
        {"controllerType": "KEBA_PD", "controllerTypeId": "KEBA_PD"},
        {"controllerType": "KEBA2COLOR", "controllerTypeId": "KEBA2COLOR"},
        {"controllerType": "KEBAAHX10", "controllerTypeId": "KEBAAHX10"},
        {"controllerType": "KEBA40", "controllerTypeId": "KEBA40"},
        {"controllerType": "KEBA", "controllerTypeId": "2KEBA"},
        {"controllerType": "KEBA2P", "controllerTypeId": "KEBA2P"},
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "KEBA",
      "controllerVendorId": "KEBA"
    },
    {
      "controllerTypeData": [
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "vendors",
      "controllerVendorId": "vendors"
    },
    {
      "controllerTypeData": [
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "其他",
      "controllerVendorId": "其他"
    },
    {
      "controllerTypeData": [
        {"controllerType": "HX", "controllerTypeId": "HX"},
        {"controllerType": "AK580", "controllerTypeId": "AK580"},
        {"controllerType": "AK680", "controllerTypeId": "AK680"},
        {"controllerType": "AK668", "controllerTypeId": "AK668"},
        {"controllerType": "HXOLD", "controllerTypeId": "HXOLD"},
        {"controllerType": "HX4S", "controllerTypeId": "HX4S"},
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "弘讯科技",
      "controllerVendorId": "弘讯科技"
    },
    {
      "controllerTypeData": [
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "富士",
      "controllerVendorId": "富士"
    },
    {
      "controllerTypeData": [
        {"controllerType": "T6F3", "controllerTypeId": "T6F3"},
        {"controllerType": "T6F5", "controllerTypeId": "T6F5"},
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "科强智控",
      "controllerVendorId": "科强智控"
    },
    {
      "controllerTypeData": [
        {"controllerType": "SIGMATEK", "controllerTypeId": "SIGMATEK"},
        {"controllerType": "其他", "controllerTypeId": "其他型号"}
      ],
      "controllerVendor": "Sigmatek",
      "controllerVendorId": "Sigmatek"
    }
  ];
}
